“”“
插件管理类负责加载和管理 Redhat 虚拟机插件。
”“”
// PluginManager.cpp
#include "PluginManager.h"
#include <iostream>
#include <cstring>

namespace ebackup {
namespace plugins {

PluginManager::PluginManager() {
    pthread_mutex_init(&pluginMutex, nullptr);
}

PluginManager::~PluginManager() {
    unloadAllPlugins();
    pthread_mutex_destroy(&pluginMutex);
}

void PluginManager::loadPlugin(const std::string& pluginPath) {
    pthread_mutex_lock(&pluginMutex);

    if (loadedPlugins.find(pluginPath) != loadedPlugins.end()) {
        std::cerr << "Plugin already loaded: " << pluginPath << std::endl;
        pthread_mutex_unlock(&pluginMutex);
        return;
    }

    void* handle = loadPluginLibrary(pluginPath);
    if (handle == nullptr) {
        pthread_mutex_unlock(&pluginMutex);
        return;
    }

    IPlugin* plugin = createPluginInstance(handle);
    if (plugin == nullptr) {
        dlclose(handle);
        pthread_mutex_unlock(&pluginMutex);
        return;
    }

    loadedPlugins[pluginPath] = { handle, plugin };

    std::cout << "Plugin loaded: " << pluginPath << std::endl;

    pthread_mutex_unlock(&pluginMutex);
}

void PluginManager::unloadPlugin(const std::string& pluginPath) {
    pthread_mutex_lock(&pluginMutex);

    auto it = loadedPlugins.find(pluginPath);
    if (it != loadedPlugins.end()) {
        it->second.plugin->shutdown();
        dlclose(it->second.handle);
        loadedPlugins.erase(it);
        std::cout << "Plugin unloaded: " << pluginPath << std::endl;
    }

    pthread_mutex_unlock(&pluginMutex);
}

void PluginManager::unloadAllPlugins() {
    pthread_mutex_lock(&pluginMutex);

    for (auto& pair : loadedPlugins) {
        pair.second.plugin->shutdown();
        dlclose(pair.second.handle);
    }
    loadedPlugins.clear();

    pthread_mutex_unlock(&pluginMutex);
}

void PluginManager::initializePlugins() {
    pthread_mutex_lock(&pluginMutex);

    for (auto& pair : loadedPlugins) {
        pair.second.plugin->initialize();
    }

    pthread_mutex_unlock(&pluginMutex);
}

void PluginManager::shutdownPlugins() {
    pthread_mutex_lock(&pluginMutex);

    for (auto& pair : loadedPlugins) {
        pair.second.plugin->shutdown();
    }

    pthread_mutex_unlock(&pluginMutex);
}

void* PluginManager::loadPluginLibrary(const std::string& pluginPath) {
    void* handle = dlopen(pluginPath.c_str(), RTLD_NOW);
    if (handle == nullptr) {
        std::cerr << "Failed to load plugin: " << dlerror() << std::endl;
    }
    return handle;
}

IPlugin* PluginManager::createPluginInstance(void* handle) {
    typedef IPlugin* (*CreatePluginFunc)();
    CreatePluginFunc createPlugin = (CreatePluginFunc)dlsym(handle, "createPlugin");
    if (createPlugin == nullptr) {
        std::cerr << "Failed to get plugin factory: " << dlerror() << std::endl;
        return nullptr;
    }
    return createPlugin();
}

} // namespace plugins
} // namespace ebackup

“”“
插件接口类
”“”
// IPlugin.h
#ifndef IPLUGIN_H
#define IPLUGIN_H

namespace ebackup {
namespace plugins {

class IPlugin {
public:
    virtual void initialize() = 0;
    virtual void shutdown() = 0;
};

} // namespace plugins
} // namespace ebackup

#endif // IPLUGIN_H

“”“
插件示例实现
”“”
// RedhatVMPlugin.cpp
#include "IPlugin.h"

class RedhatVMPlugin : public ebackup::plugins::IPlugin {
public:
    void initialize() override {
        // 插件初始化逻辑
    }

    void shutdown() override {
        // 插件关闭逻辑
    }
};

extern "C" {
    ebackup::plugins::IPlugin* createPlugin() {
        return new RedhatVMPlugin();
    }
}

“”“
插件管理主程序模块
”“”
// main.cpp
#include "PluginManager.h"

int main() {
    ebackup::plugins::PluginManager manager;

    // 加载插件
    manager.loadPlugin("libredhatvmplugin.so");
    manager.initializePlugins();

    // 执行业务逻辑
    // ...

    // 卸载插件
    manager.shutdownPlugins();
    manager.unloadAllPlugins();

    return 0;
}